<template>
  <div ref="chartWraper" :style="{ height: height }" />
</template>

<script>
// import * as echarts from "echarts";
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  name: 'BaseChart',
  mixins: [resize],

  // props: [
  //   'height',
  //   'type',
  //   'data',
  //   'color'
  // ],
  props: {
    height: {
      type: String,
      default: '100%'
    },
    type: {
      type: String,
      default: 'bar'
    },
    chartData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      chart: null
    }
  },
  // watch: {
  //   data: {
  //     deep: true,
  //     immediate: true,
  //     handler() {
  //       this.initChart();
  //     },
  //   },
  // },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  methods: {
    resize() {
      this.chart.resize()
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'chartWraper')
      this.$nextTick(() => {
        this.chart.setOption({
          tooltip: {
            trigger: 'axis'
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: {
          // 横坐标
            type: 'category',
            boundaryGap: false,
            data: this.chartData.xdata
          },
          yAxis: {
            type: 'value'
          },
          // 三条折线就有三种series，可以更改type以改变是否为折线
          series: [
            {
              name: '计划值',
              type: this.type,
              color: '#879D6E',
              data: this.chartData.adata,
              areaStyle: { opacity: 0 }
            },
            {
              name: '实际值',
              type: this.type,
              data: this.chartData.bdata,
              color: '#424EBA',
              areaStyle: { opacity: 0 }
            }
          ]
        })
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
